<?php 
class UsersController extends AppController{
  public $scaffold;
  public $components = array('File');
  public function beforeFilter() {
    if ($this->action == 'upload') {
      $this->autoRender = false;
      $this->Session->id($this->params['pass'][0]);
      $this->Session->check();
    }
    $this->Auth->allow('login','index', 'upload');
    $this->Auth->deny('addUserCategory');
    parent::beforeFilter();
  }

  public function isAuthorized($user) {
    // All registered users can add posts
    if ($this->action === 'addCategory') {
      return true;
    }

    /*
    // The owner of a post can edit and delete it
    if (in_array($this->action, array('edit', 'delete'))) {
      $postId = $this->request->params['pass'][0];
      if ($this->Post->isOwnedBy($postId, $user['id'])) {
        return true;
      }
    }
     */

    return parent::isAuthorized($user);
  }
  public function index() {
    $this->User->recursive = 0;
    $this->set('users', $this->paginate());
  }

  public function view($id = null) {
    $this->User->id = $id;
    if (!$this->User->exists()) {
      throw new NotFoundException(__('Invalid user'));
    }
    $this->set('user', $this->User->read(null, $id));
  }

  public function add() {
    if ($this->request->is('post')) {
      $this->User->create();
      if ($this->User->save($this->request->data)) {
        $this->Session->setFlash(__('The user has been saved'));
        $this->redirect(array('action' => 'index'));
      } else {
        $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
      }
    } else {
      $groups = $this->User->Group->find('list');
      $this->set(compact('groups'));
    }
  }

  public function edit($id = null) {
    $this->User->id = $id;
    if (!$this->User->exists()) {
      throw new NotFoundException(__('Invalid user'));
    }
    if ($this->request->is('post') || $this->request->is('put')) {
      if ($this->User->save($this->request->data)) {
        $this->Session->setFlash(__('The user has been saved'));
        $this->redirect(array('action' => 'index'));
      } else {
        $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
      }
    } else {
      $this->request->data = $this->User->read(null, $id);
      unset($this->request->data['User']['password']);
      $groups = $this->User->Group->find('list');
      $this->set(compact('groups'));
    }
  }

  public function delete($id = null) {
    if (!$this->request->is('post')) {
      throw new MethodNotAllowedException();
    }
    $this->User->id = $id;
    if (!$this->User->exists()) {
      throw new NotFoundException(__('Invalid user'));
    }
    if ($this->User->delete()) {
      $this->Session->setFlash(__('User deleted'));
      $this->redirect(array('action' => 'index'));
    }
    $this->Session->setFlash(__('User was not deleted'));
    $this->redirect(array('action' => 'index'));
  }
  public function login() {
    if ($this->request->is('post')) {
      $username = $this->Auth->user('username');
      $name = $this->data['User']['username'];
      if($this->Auth->user('username')!=$this->data['User']['username']){
        $this->Auth->logout();
      }
      if ($this->Auth->login()) {
        $this->redirect($this->Auth->redirect());
      } else {
        $this->Session->setFlash('Your username or password was incorrect.');
      }
    }
  }

  public function logout(){
    $this->Auth->logout();
    $this->redirect($this->Auth->redirect());
  }

  public function register() {
    if($this->data) {
      $this->request->data['User']['group_id'] = 3;
      if($this->User->save($this->request->data)) {
        $this->redirect('index');
      } else {
        $this->Session->setFlash('Save successful!');
      }
    }
  }


  public function categories() {
    $this->loadModel('Category');
    $this->paginate = array(
      'conditions' => array('Category.user_id' => $this->Auth->user('id')),
      'limit' => 10
    );
    $categories = $this->paginate('Category');
    $this->set(compact('categories'));
  }

  public function addCategory() {
    $this->loadModel('Category');
    $categories = $this->Category->generateTreeList(array('user_id'=>$this->Auth->user('id')), null, null, '--');
    foreach($categories as $key=>$value) {
      $parent[$key] = $value;
    }
    $this->set('parents', $parent);
  }

  public function addUserCategory($userId) {
    $this->loadModel('Category');
    $categories = $this->Category->find('list', array('conditions' => array('Category.parent_id'=>3)));
    $cates = array();
    foreach($categories as $key=>$value) {
      array_push($cates,array('parent_id'=>$key,'user_id'=>$userId, 'name'=>$value));
      echo $key;
    }
    $this->Category->saveMany($cates);
    echo 'done';
  }

  /* add Photo */

  public function addPhoto() {
    if($this->request->is('post')) {
      if($this->data) {
        if($this->Photo->save($this->data)) {
          $this->Session->setFlash('Upload Successful');
        } else {
          $this->Session->setFlash('Upload Failed, try again');
        }
      }
    } else {
      $this->loadModel('Category');
      $categories = $this->Category->find('list', array('conditions' => array('parent_id' => $this->Auth->user('id'))));
      $this->set(compact('categories'));
    }
  }

  public function upload() {
    $moved = move_uploaded_file($this->params['form']['Filedata']['tmp_name'], WWW_ROOT . '/files/' . $this->params['form']['Filedata']['name']);
    echo 'hello' . $this->params['form']['Filedata']['name'];
    /*
    if ($this->File->upload()) {
      // save the file to the db, or do whateve ryou want to do with the data
      $this->params['form']['Filedata']['name'] = $this->File->filename;
      $this->params['form']['Filedata']['path'] = $this->File->webpath;
      $this->params['form']['Filedata']['fspath'] = $this->File->uploadpath . $this->File->filename;
      $this->data['File'] = $this->params['form']['Filedata'];

      if (!($file = $this->File->save($this->data))){
        $this->Session->setFlash('Database save failed');
      } else {
        $this->Session->setFlash('File Uploaded: ' . $this->File->filename . '; Database id is ' . $this->File->getLastInsertId() . '.');
      }
    } else {
      $this->Session->setFlash($this->File->errorMessage);
    } 
    echo 'hello';
     */
  }

}
?>
